const SYMBOL_KIND = {
  STATIC: 'static',
  FIELD: 'field',
  ARG: 'arg',
  VAR: 'var',
  NONE: 'none'
};

class SymbolTable {
  constructor() {
    this.reset();
  }

  reset() {
    this._table = new Map();
    this._kindIndexMap = {
      [SYMBOL_KIND.STATIC]: 0,
      [SYMBOL_KIND.FIELD]: 0,
      [SYMBOL_KIND.ARG]: 0,
      [SYMBOL_KIND.VAR]: 0
    };
  }

  /**
   * 向符号表中添加一个变量
   * @param {string} name
   * @param {string} type
   * @param {SYMBOL_KIND} kind
   */
  define(name, type, kind) {
    const index = this._kindIndexMap[kind]++;
    this._table.set(name, { name, type, kind, index });
  }

  /**
   * 返回符号表中kind类型变量的数量
   * @param {string} kind
   * @returns {number}
   */
  varCount(kind) {
    return this._kindIndexMap[kind];
  }

  /**
   * 返回符号表中变量name的kind
   * @param {string} name
   * @returns {SYMBOL_KIND}
   */
  kindOf(name) {
    if (!this._table.has(name)) return;
    return this._table.get(name).kind;
  }

  /**
   * 返回符号表中变量name的type
   * @param {string} name
   * @returns {string}
   */
  typeOf(name) {
    if (!this._table.has(name)) return;
    return this._table.get(name).kind;
  }

  /**
   * 返回符号表中变量name的index
   * @param {string} name 
   * @returns {number}
   */
  indexOf(name) {
    if (!this._table.has(name)) return;
    return this._table.get(name).index;
  }
}

module.exports.SymbolTable = SymbolTable;
module.exports.SYMBOL_KIND = SYMBOL_KIND;
